<?php

/**
 * WBSplitPanel
 * 
 * <code>
 * $panel->add(array(
 *     'wbtype' => 'SplitPanel',
 *     'layout' => 'vbox'
 * ))
 * </code>
 * 
 * @author easylogic
 * @package php.ui.winbinder
 */
class WBSplitPanel extends WBPanel {
	
	const WIDTH = 10;
	const HEIGHT = 10;
	
	/**
	 * 내부 생성된 Splitter
	 * 
	 * @var WBSplitter
	 */
	private $_splitter;
	
	/**
	 * 생성자
	 * 
	 * SplitPanel 에서는 layout 을  vbox, hbox 로 지정을 하면 자동으로 splitter 가 생성된다.
	 * 두 개의 layout 만 사용가능하다.  
	 * 
	 */ 
	public function __construct($options = array()) {
		parent::__construct($options);
	}
	
	public function __destruct() { 
		parent::__destruct();
		
		unset($this->_splitter);
	}		
	
	/**
	 * WBSplitter 생성
	 * 
	 * @see WBPanel#initComponent()
	 */
	public function initComponent() {
		parent::initComponent();
		$this->createSplitter();		
	}
	
	/**
	 * 내부 Splitter 생성 
	 * 
	 */
	public function createSplitter() {
		$this->_splitter = new WBSplitter(array('parent' => $this));
	}

	/**
	 * Splitter 설정
	 * 
	 * @param WBSplitter $splitter
	 */
	public function setSplitter($splitter) { 
		$this->_splitter = $splitter;
	}

	/**
	 * Splitter 얻어오기 
	 * 
	 * @return WBSplitter
	 */
	public function getSplitter() { 
		return $this->_splitter;
	}
	
	/**
	 * rendering layout
	 * 
	 * @see WBContainer#doLayout()
	 */
	public function doLayout() {
		$bounds = $this->getBounds(true);

		$width = 0;
		$height = 0;
		$restWidth = $bounds['width'];
		$restHeight = $bounds['height'];

		$first = $this->get(0);
		$second = $this->get(1);
		
		$isInitSplitter = $this->_splitter->isInit();

		if ($first) { 
			if (isset($first->minWidth)) { 
				$this->_splitter->setOption('minX', $bounds['x'] + $first->getOption('minWidth'));
			} else { 
				$this->_splitter->setOption('minX', $bounds['x'] + self::WIDTH);
			}
			
			if (isset($first->maxWidth)) { 
				$this->_splitter->setOption('maxX', $bounds['x'] + $first->getOption('maxWidth'));
			} else { 
				$this->_splitter->setOption('maxX', $bounds['x'] + $first->getOption('width'));
			}
			
			if (isset($first->minHeight)) { 
				$this->_splitter->setOption('minY', $bounds['y'] + $first->getOption('minHeight'));
			} else { 
				$this->_splitter->setOption('minY', $bounds['y'] + self::HEIGHT);
			}
			
			if (isset($first->maxHeight)) { 
				$this->_splitter->setOption('maxY', $bounds['y'] + $first->getOption('maxHeight'));
			} else { 
				$this->_splitter->setOption('maxY', $bounds['y'] + $first->getOption('height'));
			}
		}
		
		if ($isInitSplitter) { // splitter 최초 생성일경우  
			if ($first) { 
				$firstSize = $first->getSize();
				
				if ($this->_splitter->isEnabledMoveX()) {
					$width = $firstSize['width']; 
					$height = $bounds['height'];
					
					$first->setBounds($bounds['x'], $bounds['y'], $width, $height);

					$bounds['x'] += $width;
					$restWidth -= $width;
				} else { 
					$width = $bounds['width']; 
					$height = $firstSize['height'];
					
					$first->setBounds($bounds['x'], $bounds['y'], $width, $height);

					$bounds['y'] += $height;
					$restHeight -= $height;					
				}
				
			}
			
		} else {
			$pos = $this->_splitter->getPosition();
			
			if ($first) { 
				if ($this->_splitter->isEnabledMoveX()) {
					$width = $pos['x'] - $bounds['x']; 
					$height = $bounds['height'];
					
					$first->setBounds($bounds['x'], $bounds['y'], $width, $height);

					$bounds['x'] += $width;
					$restWidth -= $width;
				} else { 
					$width = $bounds['width']; 
					$height = $pos['y'] - $bounds['y'];
					
					$first->setBounds($bounds['x'], $bounds['y'], $width, $height);

					$bounds['y'] += $height;
					$restHeight -= $height;					
				}
				
			}
		}

		if ($this->_splitter) { 
			$splitterSize = $this->_splitter->getSize();
			
			if ($this->_splitter->isEnabledMoveX()) {
				$width = $splitterSize['width']; 
				$height = $bounds['height'];
				
				$this->_splitter->setBounds($bounds['x'], $bounds['y'], $width, $height);

				$bounds['x'] += $width;
				$restWidth -= $width;					
			} else { 
				$width = $bounds['width']; 
				$height = $splitterSize['height'];
				
				$this->_splitter->setBounds($bounds['x'], $bounds['y'], $width, $height);
				
				$bounds['y'] += $height;
				$restHeight -= $height;										
			}				
		}
		
		if ($second) { 
			if ($this->_splitter->isEnabledMoveX()) {
				$width = $restWidth; 
				$height = $bounds['height'];
				
				$second->setBounds($bounds['x'], $bounds['y'], $width, $height);
			} else { 
				$width = $bounds['width']; 
				$height = $restHeight;
				
				$second->setBounds($bounds['x'], $bounds['y'], $width, $height);
			}
			
		}
			

	}
}

?>